<template>
  <div id="cornerComment" v-if="detailVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>指令调试</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form ref="form" :model="form" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="调试设备:" prop="noticeTitle">
                <el-button size="small" type="primary" plain icon="el-icon-plus" class="mb-10" @click="checkDevice()">
                  选择调试设备
                </el-button>
                {{ deviceDetail ? `【${deviceDetail.name}】` : '' }}
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="执行命令" prop="deptId">
                <el-button size="small" type="primary" plain icon="el-icon-plus" class="mb-10" @click="addList('init')">
                  新增
                </el-button>
                <div class="Json-list mb-10" v-for="(item, index) in commands" :key="index">
                  <div class="Json-list-line">
                    <span class="">{{ index + 1 }}.</span>
                    <el-input v-model="item.key_name" placeholder="key" maxlength="30" class="w-150 mr-10"
                              @input="getCommandLink"/>
                    <span v-if="item.type==1">:</span>
                    <el-input v-model="item.value_name" v-if="item.type==1" placeholder="value" maxlength="30"
                              class="w-150 ml-10" @input="getCommandLink"/>
                    <el-radio-group v-model="item.type" class="ml-10">
                      <el-radio :label="1">键值</el-radio>
                      <el-radio :label="2">JSON</el-radio>
                    </el-radio-group>
                    <el-button size="small" type="primary" class="ml-10" plain icon="el-icon-plus" v-if="item.type==2"
                               @click="addSmallList(index)">add
                    </el-button>
                    <el-button size="small" type="danger" plain icon="el-icon-delete" plain @click="deleteList(index)"
                               class="ml-10">delete
                    </el-button>
                  </div>
                  <div class="Json-small-list" v-if="item.type==2" v-for="(smallItem, smallIndex) in item.list"
                       :key="'small'+smallIndex">
                    <div class="Json-list-line">
                      <el-input v-model="smallItem.key_name" placeholder="key" maxlength="30" class="w-150 mr-10"
                                @input="getCommandLink"/>
                      <span>:</span>
                      <el-input v-model="smallItem.value_name" placeholder="value" maxlength="30" class="w-150 ml-10"
                                @input="getCommandLink"/>
                      <el-button size="small" type="danger" plain icon="el-icon-delete" plain
                                 @click="deleteSmallList(index, smallIndex)"
                                 class="ml-10">delete
                      </el-button>
                    </div>
                  </div>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="下发指令:" prop="noticeTitle">
                {{ commandLink }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-divider>设备当前信息</el-divider>
        <el-form :model="form" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="设备状态:" prop="noticeTitle">
                {{ deviceData || '无' }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button size="small" @click="closePOpup">cancel</el-button>
        <el-button size="small" type="primary" @click="downRule">
          confirm并执行
        </el-button>
      </div>
    </div>
    <deviceList :visible.sync="deviceVisible" @getDevice="getDevice"></deviceList>
  </div>
</template>

<script>
import {addCommand, updateCommand, getCommand} from "@/api/system/command";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getDevice, updateDevice} from "@/api/system/device";
import deviceList from "@/components/Device"
import {deviceCommandDown} from '@/api/system/board'

export default {
  props: ['visible', 'commandId', 'editType'],
  name: "deviceDetail",
  components: {Treeselect, deviceList},
  data() {
    return {
      detailVisible: false,
      commands: [
        {
          key_name: '',
          value_name: '',
          type: 1,
          list: []
        }
      ],
      form: {
        name: ''
      },
      // 部门树选项
      deptOptions: undefined,
      commandLink: '',
      deviceVisible: false,
      deviceDetail: null,
      deviceData: null
    }
  },
  watch: {
    visible() {
      this.detailVisible = this.visible
      if (!this.visible) {
      } else {
        if (this.commandId) {
          this.getDetailFn()
        }
      }
    }
  },
  created() {
  },
  methods: {
    downRule() {
      if(!this.deviceDetail){
        this.$message.error('选择需要调试的设备')
        return
      }
      if(!this.commandLink){
        this.$message.error('填写需要下发的命令')
        return
      }
      let data_ = {
        id: Number(this.deviceDetail.id), //  设备id
        command: [this.commandLink], // 数组
      }
      deviceCommandDown(data_).then(res=>{
        console.log('res下发指令:', res)
        this.$message.success('下发成功')
      })
    },
    getDetail() {
      getDevice(this.deviceDetail.id).then(response => {
        console.log('设备详情==>:', response)
        let {data} = response
        this.deviceData = data.data
      });
    },
    getDevice(option) {
      this.deviceData = null
      this.deviceDetail = JSON.parse(JSON.stringify(option))
      this.getDetail()
    },
    getDetailFn() {
      // this.commandId
      getCommand(this.commandId).then(response => {
        console.log('指令详情', response)
        let {data} = response
        this.form.name = data.name
        this.form.deptId = data.deptId
        let json_ = JSON.parse(data.commands)
        let list_ = []
        for (let key in json_) {
          let a_ = {
            key_name: '',
            value_name: '',
            type: 1, // 1 键值 2 json
            list: []
          }
          if (Object.prototype.toString.call(json_[key]) === '[object Object]') {
            a_.type = 2
            console.log(json_[key])
            a_.key_name = key
            for (let index_ in json_[key]) {
              a_.list.push(
                {
                  key_name: index_,
                  value_name: json_[key][index_],
                  type: 1, // 1 键值 2 json
                  list: []
                }
              )
            }
          } else {
            a_.type = 1
            a_.key_name = key
            a_.value_name = json_[key]
          }
          list_.push(a_)
        }
        console.log('list_:', list_)
        this.commands = JSON.parse(JSON.stringify(list_))
        this.getCommandLink()
      });
    },
    getCommandLink() {
      let data_ = {}
      for (let item of this.commands) {
        if (item.type === 1) {
          data_[item.key_name] = item.value_name
        } else {
          let smallJson = {}
          for (let small_ of item.list) {
            smallJson[small_.key_name] = small_.value_name
          }
          data_[item.key_name] = smallJson
        }
      }
      this.commandLink = JSON.stringify(data_)
    },
    checkDevice() {
      this.deviceVisible = true
    },
    addList(option) {
      if (option === 'init') {
        this.commands.push({
          key_name: '',
          value_name: '',
          type: 1,
          list: []
        })
        this.getCommandLink()
      }
    },
    deleteList(index) {
      this.commands.splice(index, 1)
      this.getCommandLink()
    },
    addSmallList(index) {
      this.commands[index].list.push({
        key_name: '',
        value_name: '',
        type: 1,
        list: []
      })
      this.getCommandLink()
    },
    deleteSmallList(index, small) {
      this.commands[index].list.splice(small, 1)
      this.getCommandLink()
    },
    closePOpup() {
      this.$emit('update:visible', false)
    },
  }
}
</script>

<style scoped>
.Json-list {
  border: 1px solid #eee;
  padding: 10px;
}

.Json-list-line {
  display: flex;
  align-items: center;
}

.w-150 {
  width: 150px;
}

.ml-10 {
  margin-left: 10px;
}

.mr-10 {
  margin-right: 10px;
}

.mb-10 {
  margin-bottom: 10px;
}

#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99;
}

.cornerComment-in {
  width: 800px;
  height: 80%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.Json-small-list {
  background: #eee;
  padding: 10px;
  margin-top: 10px;
}
</style>
